function out = tvmats(SET,SC)
% Compute the time-varying Qt and Gt matrices given anticipated and
% unanticipated changes in the structural parameters
%
%

ant_chg   = SET.tv_str_chg.ant_chg_params ;
unant_chg = SET.tv_str_chg.unant_chg_params ;
Tbstar    = SET.tv_str_chg.Tbstar ;

if nargin<2 ; SC.t_f = zeros(1,Tbstar) ; end

if length(SC.t_f)<Tbstar 
    extra = linspace(SC.t_f(end)-1,0,SC.t_f(end)) ;
    SC.t_f = [SC.t_f extra] ;
    SC.t_f = [SC.t_f zeros(1,Tbstar-length(SC.t_f))] ;
end

M_       = SET.M_ ;
oo_      = SET.oo_ ;
options_ = SET.options_ ;

cur_t    = SET.tv_str_chg.cur_t ;

ant_chg_ind = 1 ;

if Tbstar<cur_t ; Tbstar = cur_t ; ant_chg_ind = 0 ; end

out.unq = 1 ;

%%

Qt = repmat(SET.mats.Q,[1 1 Tbstar]) ;
Gt = repmat(SET.mats.G,[1 1 Tbstar]) ;

for tt=cur_t:Tbstar

    if ant_chg_ind
        for ii=1:length(ant_chg)
            eval(['M_.params(SET.param_names.',ant_chg{ii},...
                ')=SET.tv_str_chg.ant_chg.',ant_chg{ii},...
                '_vec(',int2str(tt),') ;']) ;
        end
    end
    
    if tt==cur_t
        for ii=1:length(unant_chg)
            eval(['M_.params(SET.param_names.',unant_chg{ii},...
                ')=SET.tv_str_chg.unant_chg.',unant_chg{ii},...
                '_vec(',int2str(tt),') ;']) ;
        end
    end

    dyn_in.M_  = M_ ;
    dyn_in.oo_ = oo_ ;
    dyn_in.options_ = options_ ;
    dyn_in.solve = 0 ;
    dyn_in.linearize_around_diff_y = 0 ;

    if tt==Tbstar ; dyn_in.solve = 1 ; end
    
    if SET.unant_demographics
        dyn_in.solve = 1 ;
    end
    
    mats = resolve_(SET,dyn_in) ;
    
    if dyn_in.solve
        if isempty(mats.mats.Q)
            out.unq = 0 ; 
            return ; 
        end
        if tt==cur_t 
            out.y_ss1 = (eye(SET.variable.n_-1) - ...
                mats.mats.Q(1:SET.variable.n_-1,1:SET.variable.n_-1)) ...
                \ mats.mats.Q(1:SET.variable.n_-1,end)  ;
        end
    end

    str_mats{tt}    = mats.str_mats ;
    mat_i_f_zlb{tt} = mats.mat_i_f_zlb ;
    zlb{tt}         = mats.zlb ;
    
    if SET.unant_demographics
        Qvec{tt}    = mats.mats.Q ;
        Gvec{tt}    = mats.mats.G ;
    end

    if tt==Tbstar ; Qf = mats.mats.Q ; Gf = mats.mats.G ; end
    
end

for tt = cur_t:Tbstar-1
    A0i(:,:,tt) = str_mats{tt}.A0 ;
    A1i(:,:,tt) = str_mats{tt}.A1 ;
    B0i(:,:,tt) = str_mats{tt}.B0 ;
    D0i(:,:,tt) = str_mats{tt}.D0 ;
end

%%

if ~SET.unant_demographics

Qt = repmat(Qf,[1 1 Tbstar]) ;
Gt = repmat(Gf,[1 1 Tbstar]) ;

for tt = Tbstar-1:-1:cur_t
    Qt(:,:,tt) = (A0i(:,:,tt)-B0i(:,:,tt)*Qt(:,:,tt+1))\A1i(:,:,tt) ;
    Gt(:,:,tt) = (A0i(:,:,tt)-B0i(:,:,tt)*Qt(:,:,tt+1))\D0i(:,:,tt) ;
end

end

if SET.unant_demographics

Qt = repmat(Qf,[1 1 Tbstar]) ;
Gt = repmat(Gf,[1 1 Tbstar]) ;

for jj=1:Tbstar
    Qt(:,:,jj) = Qvec{jj} ;
    Gt(:,:,jj) = Gvec{jj} ;
end

end

ZLB_start  = find(SET.trend_dates==2009) ;%SET.zlb_start_idx ;
ZLB_Tbstar = find(SET.trend_dates==2015.5) ;%SET.zlb_start_idx ;

for tt=ZLB_start:ZLB_Tbstar 

    if SC.t_f(tt-find(SET.trend_dates==SET.start_date)+1)>0    

        Qtmp = [] ;
        Gtmp = [] ;
        tmpA0i = [] ;
        tmpA1i = [] ;
        tmpB0i = [] ;
        tmpD0i = [] ;

        jj=SC.t_f(tt-find(SET.trend_dates==SET.start_date)+1) ;

        Qtmp(:,:,1+jj) = Qt(:,:,tt+jj) ;

        for qq = 1:jj
            tmpA0i(:,:,qq) = mat_i_f_zlb{tt+jj-qq}.A0 ;
            tmpA1i(:,:,qq) = mat_i_f_zlb{tt+jj-qq}.A1 ;
            tmpB0i(:,:,qq) = mat_i_f_zlb{tt+jj-qq}.B0 ;
            tmpD0i(:,:,qq) = mat_i_f_zlb{tt+jj-qq}.D0 ;
        end

        for ii=jj:-1:1
            Qtmp(:,:,ii) = (tmpA0i(:,:,ii)-tmpB0i(:,:,ii) * ...
                Qtmp(:,:,ii+1)) \ tmpA1i(:,:,ii) ;
            Gtmp(:,:,ii) = (tmpA0i(:,:,ii)-tmpB0i(:,:,ii) * ...
                Qtmp(:,:,ii+1)) \ tmpD0i(:,:,ii) ;
        end    

        Qt(:,:,tt) = Qtmp(:,:,1) ;
        Gt(:,:,tt) = Gtmp(:,:,1) ;

    end
    
end

if Tbstar < SET.ss
    for tt=(Tbstar+1):SET.ss
        Qt(:,:,tt) = Qt(:,:,Tbstar) ;
        Gt(:,:,tt) = Gt(:,:,Tbstar) ;
    end
end

%% Outputs

out.Qt = Qt ;
out.Gt = Gt ;

out.tv_str_chg.Qf          = Qf ;
out.tv_str_chg.str_mats    = str_mats ;
out.tv_str_chg.mat_i_f_zlb = mat_i_f_zlb ;
out.tv_str_chg.zlb         = zlb ;